﻿CREATE PROCEDURE [dbo].[UpdateMedia]
	@ID BIGINT,
	@ProviderURL NVARCHAR(500),
	@MediaDesc NVARCHAR(1000), 
	@Title NVARCHAR(1000),
	@ThumbnailHeight INT,
	@ThumbnailWidth INT,
	@HTML NVARCHAR(500),
	@AuthorName NVARCHAR(255),
	@MediaVersion NVARCHAR(255),
	@ProviderName NVARCHAR(255),
	@ThumbnailURL NVARCHAR(1500),
	@MediaType NVARCHAR(255),
	@AuthorURL NVARCHAR(500)
AS
BEGIN
	BEGIN TRY
		SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
		SET NOCOUNT ON;
		DECLARE @datacounter INT;
		DECLARE @report XML;

		UPDATE [Raw].[vwMedia]
		SET  ProviderURL     = @ProviderURL
			,MediaDesc		 = @MediaDesc
			,Title			 = @Title
			,ThumbnailHeight = @ThumbnailHeight
			,ThumbnailWidth	 = @ThumbnailWidth
			,HTML			 = @HTML
			,AuthorName		 = @AuthorName
			,MediaVersion	 = @MediaVersion
			,ProviderName	 = @ProviderName
			,ThumbnailURL	 = @ThumbnailURL
			,MediaType		 = @MediaType
			,AuthorURL		 = @AuthorURL			
			,VersionDate	 = GETDATE()
			,VersionUser	 = SYSTEM_USER		
		
		WHERE MediaID = @ID

		SELECT @datacounter = @@ROWCOUNT;
		SET @report = (SELECT	'Data Created'	AS LogMessage,
								GETDATE()		AS LogTimestamp,
								@datacounter	AS RowsAffected
								FOR XML PATH('Notification'),
								TYPE,
								ROOT('Activity'));	

		EXEC dbo.CreateLog @LogType = 'UPDATE', @TableName = 'Media', @Metadata = @report;

		RETURN 0
	END TRY
	BEGIN CATCH
		DECLARE @ErrorMessage NVARCHAR(4000);
		DECLARE @ErrorSeverity INT;
		DECLARE @ErrorState INT;
		DECLARE @ErrorTimeStamp DATETIME;
		DECLARE @ErrAction NVARCHAR(25);

		SELECT	@ErrorMessage   = ERROR_MESSAGE(),
				@ErrorSeverity  = ERROR_SEVERITY(),
				@ErrorState     = ERROR_STATE(),
				@ErrorTimeStamp = GETDATE(),
				@ErrAction      = 'UPDATE';

		SET @report = (SELECT	@ErrorMessage	AS ErrorMessage,
								@ErrAction		AS ErrorAction,
								@ErrorSeverity	AS ErrorSeverity,
								@ErrorState		AS ErrorState,
								@ErrorTimeStamp AS ErrorTimestamp 
								FOR XML PATH('Error'), 
								TYPE, 
								ROOT('ErrorLog'));		

		EXEC dbo.CreateErrorLog @ErrorType = 'ERROR', @ErrorAction = @ErrAction, @TableName = 'Media', @Metadata = @report;
	END CATCH
END